import 'package:appflutterhomeschool/common/common.dart';
import 'package:appflutterhomeschool/route/application.dart';
import 'package:appflutterhomeschool/route/routes.dart';
import 'package:appflutterhomeschool/utils/sp_util.dart';
import 'package:common_utils/common_utils.dart';
import 'package:flukit/flukit.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:rxdart/rxdart.dart';

///用户：Nine Tailed Fox
///时间：2020/6/8
///邮箱：1037438704@qq.com
///功能：闪屏页面
class SplashPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return SplashPageState();
  }
}

class SplashPageState extends State<SplashPage> {
  int _status = 0;
  List<Widget> _bannerList = new List();

  List<String> _guideList = [
    "assets/images/icon_guide1.jpg",
    "assets/images/icon_guide2.jpg",
    "assets/images/icon_guide3.jpg",
  ];

  @override
  void initState() {
    super.initState();
    _init();
  }

  _init() {
    Observable.just(1).delay(new Duration(milliseconds: 500)).listen((_) {
      if (SpUtil.getBool(Constant.key_guide, defValue: true) &&
          ObjectUtil.isNotEmpty(_guideList)) {
        SpUtil.putBool(Constant.key_guide, false);
        _initBannerData();
        setState(() {
          _status = 2;
        });
      } else {
        _goMain();
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.instance = ScreenUtil(width: 1080, height: 1920)..init(context);
    return Material(
      child: Stack(
        children: <Widget>[
          _buildSplashBg(),
          Offstage(
            offstage: !(_status == 2),
            child: ObjectUtil.isEmpty(_bannerList)
                ? new Container()
                : new Swiper(
                    autoStart: false,
                    circular: false,
                    indicator: CircleSwiperIndicator(
                      radius: 4.0,
                      padding: EdgeInsets.only(bottom: 30.0),
                      itemColor: Colors.black26,
                    ),
                    children: _bannerList),
          ),
        ],
      ),
    );
  }

  Widget _buildSplashBg() {
    return Image.asset(
      "assets/images/icon_flash_screen.jpg",
      width: double.infinity,
      fit: BoxFit.fill,
      height: double.infinity,
    );
  }

  void _goMain() {
    Navigator.pop(context);
    if (Constant.getToken() == null || Constant.getToken() == "") {
      Application().navigatePushToPage(context, Routes.loginPage, {});
    } else {
      if (Constant.getRole() == null || Constant.getRole() == "") {
        Application().navigatePushToPage(
            context, Routes.switchIdentityPage, {"pageTag": 0});
      } else {
        Application().navigatePushToPage(context, Routes.mainTabPage, {});
      }
    }
  }

  void _initBannerData() {
    for (int i = 0, length = _guideList.length; i < length; i++) {
      if (i == length - 1) {
        _bannerList.add(new Stack(
          children: <Widget>[
            new Image.asset(
              _guideList[i],
              fit: BoxFit.fill,
              width: double.infinity,
              height: double.infinity,
            ),
            new Align(
              alignment: Alignment.bottomCenter,
              child: new Container(
                margin: EdgeInsets.only(bottom: 160.0),
                child: new InkWell(
                  onTap: () => _goMain(),
                  child: new CircleAvatar(
                    radius: 48.0,
                    backgroundColor: Colors.indigoAccent,
                    child: new Padding(
                      padding: EdgeInsets.all(2.0),
                      child: new Text(
                        '立即体验',
                        textAlign: TextAlign.center,
                        style: TextStyle(color: Colors.white, fontSize: 16.0),
                      ),
                    ),
                  ),
                ),
              ),
            ),
          ],
        ));
      } else {
        _bannerList.add(new Image.asset(
          _guideList[i],
          fit: BoxFit.fill,
          width: double.infinity,
          height: double.infinity,
        ));
      }
    }
  }
}
